import { reactive } from "vue";
import { InIitem } from "../components/script/InItem";
import { useUser } from "@/stores/user";
import { toast } from "@/components/toast/toast";
import { SignInSuc, SignIn } from "@/components/y-signIn";
import { energy } from "../script/energy";
import { getData } from "../../gotten/script/gotten";
import OpenLoding from "@/comm/openLoding";
import { Yanim } from "@/components/y-anim/script/yanim";
const checkin = reactive<CheckinType>({
    mission: [],
    keepSignDays: 0,
    totalEnergy: 0,
    energy: 0,
    signIn: false,
    getClocked: async function () {
        const user = useUser();
        const data: Checkin = await user.getClocked();
        this.mission = [];
        data.missions.forEach(item => {
            this.mission.push(new InIitem(item.action, false, item.energyAmount));
        });
        // console.log('yzt签到数据', data);

        this.signIn = data.signIn;//今天是否簽到
        this.keepSignDays = data.keepSignDays;//連續簽到天數

        // if(true){
        //     this.signIn = true;//今天是否簽到
        //     this.keepSignDays = 6;//連續簽到天數
        // }
       

        this.energy = data.missions[this.keepSignDays % 7].energyAmount; //设置今天签到能拿到的能量
        if (this.keepSignDays > 0) {//签到状态
            // this.mission[data.keepSignDays - 1].checkState = true;
            this.mission.slice(0, this.keepSignDays).forEach(item => item.checkState = true);
        }

        if(this.keepSignDays == 7 && this.signIn){
            this.mission.forEach(i=>i.checkState=true)
            this.mission[0].checkState = false
        }

        console.log("@@@@ this.mission ########", this.mission)
    },
    toSign: async function () {
        const user = useUser();
        if (user.isLogin) {
            let err = await user.toSign();
            if (err) {
                toast(err);
            } else {
                // SignInSuc(this.energy);
                // SignIn.open(this.energy);
                Yanim.active(this.energy);
                this.getClocked();
                getData(); //签到成功更新能量列表接口
                energy.getuserEnergyCount();

                user.getUserInfo();
                user.getEnergyFlow();
                user.getuserEnergyCount();
            }
        } else {
            OpenLoding();
        }
    }
});

function setTotalEnergy(energy: number = 0) {
    checkin.totalEnergy = energy;
}

export { checkin, setTotalEnergy };

type CheckinType = {
    mission: InIitem[];
    signIn: boolean;
    energy: number;
    totalEnergy: number;
    keepSignDays: number;
    getClocked: () => void;
    toSign: () => void;
}

interface Checkin {
    state: number;
    missions: Mission[];
    signIn: boolean;
    keepSignDays: number;
}

interface Mission {
    id: number;
    energyAmount: number;
    action: string;
    isUse: number;
    isRepeat: number;
    type: string;
}